<template>
	<view :style="colorStyle">
		<view class='productList'>
			<view class='search bg-color acea-row row-between-wrapper'>
				<view class="home acea-row row-center-wrapper" @click="moreNav">
					<!-- <view class="iconfont icon-fanhui2"></view>
					<view class="line"></view> -->
					<view class="iconfont icon-gengduo2"></view>
				</view>
				<!-- <view class='input acea-row row-between-wrapper'><text class='iconfont icon-sousuo'></text>
					<input placeholder='搜索商品名称' placeholder-class='placeholder' confirm-type='search' name="search"
						:value='where.keyword' @confirm="searchSubmit"></input>
				</view> -->
				<view class='iconfont' :class='is_switch==true?"icon-pailie":"icon-tupianpailie"'><!-- @click='Changswitch' -->
				</view>
			</view>
			<view class='nav acea-row row-middle row-around'>
				<view class='item line1' :class='title ? "font-num":""' @click='set_where(1)'>{{title ? title:'默认'}}
				</view>
				<view class='item' @click='set_where(2)'>
					价格
					<image v-if="price==1" src='../../static/images/up.png'></image>
					<image v-else-if="price==2" src='../../static/images/down.png'></image>
					<image v-else src='../../static/images/horn.png'></image>
				</view>
				<view class='item' @click='set_where(3)'>
					销量
					<image v-if="stock==1" src='../../static/images/up.png'></image>
					<image v-else-if="stock==2" src='../../static/images/down.png'></image>
					<image v-else src='../../static/images/horn.png'></image>
				</view>
				<!-- down -->
				<!-- <view class='item' :class='nows ? "font-color":""' @click='set_where(4)'>新品</view> -->
				<view class='item' v-if="brandList.length" :class="{clored:brandArray.length>0}" @click='set_brand'>品牌<image src='../../static/images/xiala.png'></image></view>
				<homeList :navH="navH" :goodList="goodList" :currentPage="currentPage" :sysHeight="sysHeight" :goodsShow="true"></homeList>
			</view>
			<view class='listBig acea-row row-between-wrapper' :class='is_switch==true?"":"on"'>
				<view class='itemBig' :class='is_switch==true?"":"on"' hover-class='none'
					v-for="(item,index) in productList" :key="index" @click="godDetail(item)">
					<view class='pictrue img-box' :class='is_switch==true?"":"on"'>
						<image :src='item.image' :class='is_switch==true?"":"on"'></image>
						<span class="pictrue_log_class" :class="is_switch === true ? 'pictrue_log_big' : 'pictrue_log'"
							v-if="item.activity && item.activity.type === '1'">抢单</span>
						<span class="pictrue_log_class" :class="is_switch === true ? 'pictrue_log_big' : 'pictrue_log'"
							v-if="item.activity && item.activity.type === '2'">砍价</span>
						<span class="pictrue_log_class" :class="is_switch === true ? 'pictrue_log_big' : 'pictrue_log'"
							v-if="item.activity && item.activity.type === '3'">拼团</span>
					</view>
					<view class='text' :class='is_switch==true?"":"on"' style="display: flex; flex-direction: column; justify-content: space-between;">
						<view class='name line2 fengexian'>{{item.store_name}}</view>
						<!-- <view class='money font-color' :class='is_switch==true?"":"on"'>￥<text
								class='num'>{{item.price}}</text></view> -->
						<!-- <view class='vip acea-row row-between-wrapper' :class='is_switch==true?"":"on"'>
							<view class='vip-money' v-if="item.vip_price && item.vip_price > 0">￥{{item.vip_price}}
								<image src='../../static/images/vip.png'></image>
							</view>
							<view>已售{{item.sales}}{{item.unit_name || '件'}}</view>
						</view> -->
						<!-- <view class="name line2 fengexian">
							<span class="coupon" :style="'border:1px solid '+labelColor+';color:'+labelColor" v-if="item.checkCoupon && couponShow">券</span>
							<span v-if="titleShow">{{item.store_name}}</span>
						</view> -->
						 <view class="introduce_bottom" :style="'color:'+fontColor" v-if="item.touzi_shouyi_bill_arr.length && item.touzi_month_arr.length && item.touzi_shouyi_arr.length">
							 
							 <view v-if="item.touzi_month_arr">
								<view class="title">
									{{item.touzi_month_arr[1]}}
									
								</view>
								<view class="msg">
									{{item.touzi_month_arr[0]}}
								</view>
							 </view>
							 <view v-if="item.touzi_shouyi_bill_arr">
								<view class="title">
									{{item.touzi_shouyi_bill_arr[1]}}
								</view>
								<view class="msg">
									{{item.touzi_shouyi_bill_arr[0]}}
								</view>
							 </view>
							 <view v-if="item.touzi_shouyi_arr">
								<view class="title">
									{{item.touzi_shouyi_arr[1]}}
								</view>
								<view class="msg">
									{{item.touzi_shouyi_arr[0]}}
								</view>
							 </view>
							 <view v-if="item.price">
								<view class="title">
									{{item.price}}
								</view>
								<view class="msg">
									采购价格
								</view>
							 </view>
					
						  </view>
						  <view class="price fengexian" :style="'color:'+fontColor" v-else><span v-if="priceShow">￥<span class="num">{{item.price}}</span></span><span class="old-price"
							 v-if="opriceShow">¥{{item.ot_price}}</span>
						   </view>
						   
						<view class="" style="border-top: 2px solid #f0f0f0;" v-if="item.merch_name">
							
							<view class="caigouheader">
								<view class="caigoujindu titleStyle">
									{{item.merch_name}}
								</view>
								<view class="caigoujindu gobutton">
									加入养殖
								</view>
							</view>
							<!-- <view class="progress">
								<view class='bg-reds' :style="'width:'+item.percent+'%;'"></view>
								<view class='piece'>已抢{{item.percent}}%</view>
							</view> -->
						</view>
						
					</view>
				</view>
				<view class='loadingicon acea-row row-center-wrapper' v-if='productList.length > 0'>
					<text class='loading iconfont icon-jiazai' :hidden='loading==false'></text>{{loadTitle}}
				</view>
			</view>
		</view>
		<view class='noCommodity' v-if="productList.length==0 && where.page > 1">
			<view class='pictrue'>
				<image src='../../static/images/noShopper.png'></image>
			</view>
			<recommend :hostProduct="hostProduct"></recommend>
		</view>
		<home v-if="navigation"></home>
		<view class="mask" @touchmove.prevent :hidden="brandtip === false" @click="closeBrand"></view>
		<view class="selectbrand" :class="brandtip === true ? 'on' : ''">
			<view class="box">
				<view class="selet">
					<view class="seletbox acea-row row-center-wrapper" :class="{seleton:brandArray.indexOf(item.id) != -1}" v-for="(item,index) in brandList" :key="index" @click="seletBrand(item.id)">{{item.brand_name}}</view>
				</view>

				<view class="btn">
					<div class="sambox reset" @click="brandReset">重置</div>
					<div class="sambox ok" @click="brandOk">确定</div>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import home from '@/components/home';
	import homeList from '@/components/homeList'
	import {
		getProductslist,
		getProductHot,
		brand
	} from '@/api/store.js';
	import recommend from '@/components/recommend';
	import {
		mapGetters
	} from "vuex";
	import {
		goShopDetail
	} from '@/libs/order.js'
	import colors from '@/mixins/color.js';
	export default {
		computed: mapGetters(['uid']),
		components: {
			recommend,
			home,
			homeList
		},
		mixins:[colors],
		data() {
			return {
				navH: 22,
				sysHeight: 0,
				goodList: true,
				currentPage:false,
				brandtip: false,//品牌弹窗
				brandArray: [],
				productList: [],
				is_switch: false,
				where: {
					sid: 0,
					keyword: '',
					priceOrder: '',
					salesOrder: '',
					news: 0,
					page: 1,
					limit: 20,
					cid: 0,
					brand_id:[]
				},
				price: 0,
				stock: 0,
				nows: false,
				loadend: false,
				loading: false,
				loadTitle: '加载更多',
				title: '',
				hostProduct: [],
				hotPage: 1,
				hotLimit: 10,
				hotScroll: false,
				brandList:[]
			};
		},
		onLoad: function(options) {
			this.where.cid = options.cid || 0;
			this.$set(this.where, 'sid', options.sid || 0);
			this.title = options.title || '';
			this.title = options.title || '';
			this.$set(this.where, 'keyword', options.searchValue || '');
			this.$set(this.where, 'productId', options.productId || '');
			this.get_product_list();
			// this.getBrand();
		},
		methods: {
			moreNav(){
				this.currentPage = !this.currentPage
			},
			// 品牌列表
			getBrand(){
				brand(this.where).then(res=>{
					this.brandList = res.data
				}).catch(err=>{
					return this.$util.Tips({
						title: err.msg
					});
				})
			},
			//打开品牌弹窗
			set_brand(){
				this.brandtip = true
				this.currentPage = false
			},
			//关闭品牌/新品弹窗
			closeBrand(){
				this.brandtip = false
			},
			// 品牌选择
			seletBrand(id){
				if(this.brandArray.indexOf(id) == -1){
					this.brandArray.push(id)
				}else {
					this.brandArray.splice(this.brandArray.indexOf(id),1)
				}
			},
			brandReset(){
				this.brandArray = []
				this.brandOk();
			},
			brandOk(){
				this.brandtip = false;
				this.$set(this.where, 'brand_id', this.brandArray.join(","));
				this.loadend = false;
				this.$set(this.where, 'page', 1);
				this.get_product_list(true);
			},
			// 去详情页
			godDetail(item) {
				this.currentPage = false
				goShopDetail(item, this.uid).then(res => {
					uni.navigateTo({
						url: `/pages/goods_details/index?id=${item.id}`
					})
				})
			},
			Changswitch: function() {
				let that = this;
				this.currentPage = false
				that.is_switch = !that.is_switch
			},
			searchSubmit: function(e) {
				let that = this;
				this.currentPage = false
				that.$set(that.where, 'keyword', e.detail.value);
				that.loadend = false;
				that.$set(that.where, 'page', 1)
				this.get_product_list(true);
			},
			/**
			 * 获取我的推荐
			 */
			get_host_product: function() {
				let that = this;
				if (that.hotScroll) return
				getProductHot(
					that.hotPage,
					that.hotLimit,
				).then(res => {
					that.hotPage++
					that.hotScroll = res.data.length < that.hotLimit
					that.hostProduct = that.hostProduct.concat(res.data)
					// that.$set(that, 'hostProduct', res.data)
				});
			},
			//点击事件处理
			set_where: function(e) {
				this.currentPage = false
				switch (e) {
					case 1:
						// #ifdef H5
						return history.back();
						// #endif
						// #ifndef H5
						return uni.navigateBack({
							delta: 1,
						})
						// #endif
						break;
					case 2:
						if (this.price == 0) this.price = 1;
						else if (this.price == 1) this.price = 2;
						else if (this.price == 2) this.price = 0;
						this.stock = 0;
						break;
					case 3:
						if (this.stock == 0) this.stock = 1;
						else if (this.stock == 1) this.stock = 2;
						else if (this.stock == 2) this.stock = 0;
						this.price = 0
						break;
					case 4:
						this.nows = !this.nows;
						break;

				}
				this.loadend = false;
				this.$set(this.where, 'page', 1);
				this.get_product_list(true);
			},
			//设置where条件
			setWhere: function() {
				if (this.price == 0) this.where.priceOrder = '';
				else if (this.price == 1) this.where.priceOrder = 'asc';
				else if (this.price == 2) this.where.priceOrder = 'desc';
				if (this.stock == 0) this.where.salesOrder = '';
				else if (this.stock == 1) this.where.salesOrder = 'asc';
				else if (this.stock == 2) this.where.salesOrder = 'desc';
				this.where.news = this.nows ? 1 : 0;
			},
			//查找产品
			get_product_list: function(isPage) {
				let that = this;
				that.setWhere();
				if (that.loadend) return;
				if (that.loading) return;
				if (isPage === true) that.$set(that, 'productList', []);
				that.loading = true;
				that.loadTitle = '';
				getProductslist(that.where).then(res => {
					let list = res.data;
					let productList = that.$util.SplitArray(list, that.productList);
					let loadend = list.length < that.where.limit;
					that.loadend = loadend;
					that.loading = false;
					that.loadTitle = loadend ? '已全部加载' : '加载更多';
					that.$set(that, 'productList', productList);
					that.$set(that.where, 'page', that.where.page + 1);
					if (!that.productList.length) this.get_host_product();
				}).catch(err => {
					that.loading = false;
					that.loadTitle = '加载更多';
				});
			},
		},
		onPullDownRefresh() {

		},
		onPageScroll(e){
			this.currentPage = false
		},
		onReachBottom() {
			if (this.productList.length > 0) {
				this.get_product_list();
			} else {
				this.get_host_product();
			}

		}
	}
</script>

<style scoped lang="scss">
	.home {
		color: #333;
		width: 56rpx;
		height: 56rpx;
		background: rgba(255, 255, 255, 1);
		border: 1px solid rgba(0,0,0,0.1);
		border-radius: 40rpx;
		font-size: 33rpx;
	
		&.on {
			background: unset;
			color: #333;
		}
		
		&.homeIndex{
			width: 98rpx;
		}
	}
	
	.home .iconfont {
		width: 60rpx;
		text-align: center;
		font-size: 28rpx;
		font-weight: bold;
	}
	
	.home .line {
		width: 1rpx;
		height: 34rpx;
		background: #B3B3B3;
	}
	
	.home .icon-xiangzuo {
		font-size: 28rpx;
	}
	.clored{
		color: var(--view-theme);
		font-weight: 600;
		.icon-gou{
			font-weight: 400!important;
		}
	}
	.selectbrand{
		position: fixed;
		background-color: #FFFFFF;
		z-index: 8;
		width: 100%;
		top: 170rpx;
		left: 0;
		max-height: 860rpx;
		overflow: hidden;
		overflow-y: auto;
		border-radius: 0 0 24rpx 24rpx;
		transform: translate3d(0, -100%, 0);
		transition: all .2s cubic-bezier(.9, .5, .5, .25);
		// padding-top: 200rpx;
		// padding-top: calc(200rpx+ constant(safe-area-inset-bottom)); ///兼容 IOS<11.2/
		// padding-top: calc(200rpx + env(safe-area-inset-bottom)); ///兼容 IOS>11.2/
		.box{
			.selet{
				display: flex;
				flex-wrap: wrap;
				padding: 20rpx 20rpx 0 20rpx;
				max-height: 520rpx;
				overflow-x: hidden;
				overflow-y: auto;
				.seletbox{
					width: 216rpx;
					height: 68rpx;
					border-radius: 34rpx;
					background-color: #F5F5F5;
					margin-bottom: 30rpx;
					text-align: center;
					font-size: 24rpx;
					margin-right: 28rpx;
					&:nth-child(3n) {
						margin-right: 0;
					}
				}
				.seleton{
					border: 1px solid var(--view-theme);
					background: var(--view-minorColorT);
					color: var(--view-theme);
				}
			}
			.btn{
				display: flex;
				justify-content: space-between;
				text-align: center;
				padding: 30rpx 20rpx 40rpx;
				.sambox{
					width: 328rpx;
					height: 64rpx;
					border-radius: 38rpx;
					border: 2rpx solid var(--view-theme);
					color: #FFFFFF;
					line-height: 64rpx;
				}
				.reset{
					color: var(--view-theme);
				}
				.ok{
					background: var(--view-theme);
				}
			}
			.tlebox{
				width: 100%;
				height: 88rpx;
				line-height: 88rpx;
				display: flex;
				justify-content: space-between;
				padding: 0 40rpx 0 36rpx;
				margin-left: 20rpx;
				border-bottom: 2rpx solid #EEEEEE;
				
			}
			.tlebox:last-child{
				border-bottom: 0;
			}
		}
	}
	.selectbrand.on{
		transform: translate3d(0, 0, 0);
	}
	.productList .search {
		width: 100%;
		height: 86rpx;
		padding-left: 23rpx;
		box-sizing: border-box;
		position: fixed;
		left: 0;
		top: 0;
		z-index: 9;
	}

	.productList .search .input {
		width: 560rpx;
		height: 60rpx;
		background-color: #fff;
		border-radius: 50rpx;
		padding: 0 20rpx;
		box-sizing: border-box;
	}

	.productList .search .input input {
		width: 466rpx;
		height: 100%;
		font-size: 26rpx;
	}

	.productList .search .input .placeholder {
		color: #999;
	}

	.productList .search .input .iconfont {
		font-size: 35rpx;
		color: #555;
	}

	.productList .search .icon-pailie,
	.productList .search .icon-tupianpailie {
		color: #fff;
		width: 62rpx;
		font-size: 40rpx;
		height: 86rpx;
		line-height: 86rpx;
	}

	.productList .nav {
		height: 86rpx;
		color: #454545;
		position: fixed;
		left: 0;
		width: 100%;
		font-size: 28rpx;
		background-color: #fff;
		margin-top: 86rpx;
		top: 0;
		z-index: 9;
	}

	.productList .nav .item {
		width: 25%;
		text-align: center;
	}

	.productList .nav .item.font-color {
		font-weight: bold;
	}

	.productList .nav .item image {
		width: 15rpx;
		height: 19rpx;
		margin-left: 10rpx;
	}

	.productList .list {
		padding: 0 20rpx;
		margin-top: 172rpx;
	}

	.productList .list.on {
		background-color: #fff;
		border-top: 1px solid #f6f6f6;
	}

	.productList .list .item {
		width: 345rpx;
		margin-top: 20rpx;
		background-color: #fff;
		border-radius: 20rpx;
	}

	.productList .list .item.on {
		width: 100%;
		display: flex;
		border-bottom: 1rpx solid #f6f6f6;
		padding: 30rpx 0;
		margin: 0;
	}

	.productList .list .item .pictrue {
		position: relative;
		width: 100%;
		height: 345rpx;
	}

	.productList .list .item .pictrue.on {
		width: 180rpx;
		height: 180rpx;
	}

	.productList .list .item .pictrue image {
		width: 100%;
		height: 100%;
		border-radius: 20rpx 20rpx 0 0;
	}

	.productList .list .item .pictrue image.on {
		border-radius: 6rpx;
	}

	.productList .list .item .text {
		padding: 20rpx 17rpx 26rpx 17rpx;
		font-size: 30rpx;
		color: #222;
	}

	.productList .list .item .text.on {
		width: 508rpx;
		padding: 0 0 0 22rpx;
	}

	.productList .list .item .text .money {
		font-size: 26rpx;
		font-weight: bold;
		margin-top: 8rpx;
	}

	.productList .list .item .text .money.on {
		margin-top: 50rpx;
	}

	.productList .list .item .text .money .num {
		font-size: 34rpx;
	}

	.productList .list .item .text .vip {
		font-size: 22rpx;
		color: #aaa;
		margin-top: 7rpx;
	}

	.productList .list .item .text .vip.on {
		margin-top: 12rpx;
	}

	.productList .list .item .text .vip .vip-money {
		font-size: 24rpx;
		color: #282828;
		font-weight: bold;
	}

	.productList .list .item .text .vip .vip-money image {
		width: 46rpx;
		height: 21rpx;
		margin-left: 4rpx;
	}

	.noCommodity {
		background-color: #fff;
		padding-bottom: 30rpx;
	}
	.listBig {
		padding: 0 20rpx;
		margin-top: 172rpx;
		// padding: 0px 10rpx 10rpx 10rpx;
		.borderRadius15{
			image{
				border-radius: 20rpx 20rpx 0 0;
			}
		}
	
		.itemBig {
			width: 100%;
			margin-top: 20rpx;
			background-color: #fff;
			padding-bottom: 15rpx;
			
			.img-box {
				width: 100%;
				height: 400rpx;
				position: relative;
				padding-top: 15rpx;
				.pictrue_log_big{
					border-radius: 20rpx 0 20rpx 0;
				}
				image {
					width: 100%;
					height: 100%;
				}
			}
	
			.name {
				overflow: hidden;
				-webkit-line-clamp: 2;
				display: -webkit-box;
				-webkit-box-orient: vertical;
				text-overflow: ellipsis;
				font-size: 28rpx;
				font-weight: bold;
				margin-top: 16rpx;
				padding: 0 8px;
				// height: 80rpx;
				// line-height: 80rpx;
				.coupon {
					display: inline-block;
					text-align: center;
					width: 32rpx;
					border-radius: 4rpx;
					font-size: 20rpx;
					font-weight: normal;
					margin-right: 10rpx;
				}
			}
	
			.price {
				font-weight: bold;
				font-size: 12px;
				margin-top: 10rpx;
				padding: 0 8px;
	
				.num {
					font-size: 32rpx;
					margin-right: 10rpx;
				}
	
				.old-price {
					color: #aaa;
					font-weight: normal;
					text-decoration: line-through;
				}
			}
		}
	}
	
	.caigouheader{
		width: 100%;
		display: flex;
		justify-content: space-between;
		align-items: center;
		height: 60rpx;
		line-height: 60rpx;
		padding-left: 20rpx;
		margin-top: 20rpx;
		.caigoujindu{
			text-align: center;
			// width: 33%;
			height: 100%;
		}
		.gobutton{
			width: 25%;
			background: #02b7ec;
			color: white;
			margin-right: 20px;
			border-radius: 20rpx;
			// padding-right: 30rpx;
		}
	}
	.introduce_bottom {
		width: 100%;
		margin-top: 29rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		border-top: 1px solid #e2e7ea;
		
		>view {
			padding: 20rpx 0;
			text-align: center;
			width: 33%;
			height: 100%;
	
			&:not(:last-child) {
				// border-right: 1px solid #ededed;
			}
	
			.title {
				font-size: 30rpx;
				color: #333333;
				font-weight: bold;
			}
	
			.msg {
				// margin-top: 15rpx;
				font-size: 24rpx;
				color: #999999;
			}
		}
	}
	.fengexian{
		width: 100%;
		// height: 180rpx;
		// line-height: 180rpx;
		border-top: 2px solid #f0f0f0;
	}
	.progress {
		overflow: hidden;
		background-color: #aaa7a7;
		width: 100%;
		border-radius: 18rpx;
		height: 40rpx;
		position: relative;
		margin-top: 20rpx;
	}
	
	.progress .bg-reds {
		width: 0;
		height: 100%;
		transition: width 0.6s ease;
		background: linear-gradient(90deg, rgba(233, 51, 35, 1) 0%, rgba(255, 137, 51, 1) 100%);
	}
	
	.progress .piece {
		position: absolute;
		left: 8%;
		transform: translate(0%, -50%);
		top: 49%;
		font-size: 16rpx;
		color: #ffffff;
	}
	.titleStyle{
		color: #02b7ec;
		font-size: 16px;
		font-weight: 900;
	}
</style>